#!/bin/sh /etc/rc.common
# OpenWrt 静态ARP绑定 启动脚本
# Copyright (C) 2015 GuoGuo <gch981213@gmail.com>

START=80
STOP=20

#清除单接口ARP
#参数：$1:接口名称
if_clean_arp()
{
	[ -z "$1" ] && return
	ip link set arp off dev $1
	ip link set arp on dev $1
}

#清除系统所有ARP
#参数：无
clean_arp()
{
	for i in $(ls /sys/class/net)
	do
		if_clean_arp $i
	done
}

#添加静态ARP绑定
#参数：$1:IP地址 $2:MAC地址 $3:接口名称
add_arp()
{
	[ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] && return
	echo "Adding ARP:IP Addr:$1  MAC Addr:$2  Interface:$3"
	ip neigh add $1 lladdr $2 nud permanent dev $3 || \
	ip neigh change $1 lladdr $2 nud permanent dev $3
}

arpconf_foreach()
{
	config_get ipaddr "$1" 'ipaddr'
	config_get macaddr "$1" 'macaddr'
	config_get ifname "$1" 'ifname'
	[ -z "$ifname" ] && return
	add_arp $ipaddr $macaddr $ifname
}

start()
{
	config_load 'arpbind'
	config_foreach arpconf_foreach 'arpbind'
}

stop()
{
	clean_arp
}
